<?php

namespace app\admin\model;

use think\Model;

class About extends Model
{
    public function getCreatedTimeAttr($time)
    {
        return date('Y-m-d H:i:s', $time);
    }

    public function getUpdatedTimeAttr($time)
    {
        return date('Y-m-d H:i:s', $time);
    }

    public function getPubTimeAttr($time)
    {
        return date('Y-m-d H:i:s', $time);
    }

    public function articleList($where, $page, $size, $field = [])
    {
        $list = $this->field($field)
            ->where($where)
            ->order('pub_time', 'desc')
            ->paginate($size, true, ['page' => $page])
            ->toArray();

        return $list['data'];
    }

    public function getOne($where, $field = '*', $order = '')
    {
        return $this->field($field)->where($where)->order($order)->find()->toArray();
    }

    //修改文章资料
    public function editAbout($id, $data)
    {
        return $this->where(array('id' => $id))->update($data);
    }

    public function getAbout($num = 20)
    {
        $keywords = input('param.keywords', '');
        $where = "";
        $param = [];
        if ($keywords) {
            $param['keywords'] = $keywords;
            $where .= "title like" . "'%" . $keywords . "%'";
        }

        $about = About::where($where)
            ->order('id', 'DESC')
            ->paginate($num, false, ['query' => $param]);

        $page = $about->render();// 获取分页显示

        if ($about) {
            return ['code' => 1, 'data' => $about, 'msg' => '数据查询成功', 'page' => $page];
        } else {
            return ['code' => 2, 'data' => '', 'msg' => '暂无数据', 'page' => ''];
        }
    }

    public function delAbout($id)
    {
        return $this->where(array('id' => $id))->delete();
    }
}
